<?php
declare (strict_types = 1);

namespace app\common\service;

use think\facade\Request;
use app\common\model\ZadanConfig as M;
use app\common\validate\ZadanConfig as V;

class ZadanConfig
{
    // 添加
    public static function goAdd($data)
    {
        //验证
        $validate = new V;
        if(!$validate->scene('add')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
            M::create($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }
    
    // 编辑
    public static function goEdit($data,$id)
    {
        $data['id'] = $id;
        //验证
        $validate = new V;
        if(!$validate->scene('edit')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
             M::update($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 状态
    public static function goStatus($data,$id)
    {
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            $model->save([
                'status' => $data,
            ]);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 删除
    public static function goRemove($id)
    {
        $model = M::find($id);
        if ($model->isEmpty()) return ['msg'=>'数据不存在','code'=>201];
        try{
           $model->delete();
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids)
    {
        if (!is_array($ids)) return ['msg'=>'数据不存在','code'=>201];
        try{
            M::destroy($ids);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 获取列表
    public static function goRecycle()
    {
        if (Request::isPost()){
            $ids = Request::param('ids');
            if (!is_array($ids)) return ['msg'=>'参数错误','code'=>'201'];
            try{
                if(Request::param('type')){
                    $data = M::onlyTrashed()->whereIn('id', $ids)->select();
                    foreach($data as $k){
                        $k->restore();
                    }
                }else{
                    M::destroy($ids,true);
                }
            }catch (\Exception $e){
                return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
            }
            return ['msg'=>'操作成功'];
        }
        //按用户名
        $where = [];
        $limit = input('get.limit');
        
               //按活动是否开启(1.开启  0.关闭)查找
               if ($activiteisopen = input("activiteisopen")) {
                   $where[] = ["activiteisopen", "like", "%" . $activiteisopen . "%"];
               }
               //按玩法介绍内容查找
               if ($desc = input("desc")) {
                   $where[] = ["desc", "like", "%" . $desc . "%"];
               }
               //按开启连抽类型列表查找
               if ($typels = input("typels")) {
                   $where[] = ["typels", "like", "%" . $typels . "%"];
               }
               //按兑换比例查找
               if ($dhbl = input("dhbl")) {
                   $where[] = ["dhbl", "like", "%" . $dhbl . "%"];
               }
               //按日榜单发放物品查找
               if ($daysendgoodsls = input("daysendgoodsls")) {
                   $where[] = ["daysendgoodsls", "like", "%" . $daysendgoodsls . "%"];
               }
               //按总榜单发放物品查找
               if ($allsendgoodsls = input("allsendgoodsls")) {
                   $where[] = ["allsendgoodsls", "like", "%" . $allsendgoodsls . "%"];
               }
               //按主播在后台发放数量限制查找
               if ($sendcoinlimit = input("sendcoinlimit")) {
                   $where[] = ["sendcoinlimit", "like", "%" . $sendcoinlimit . "%"];
               }
               //按多少金币开启一次查找
               if ($onenumcoin = input("onenumcoin")) {
                   $where[] = ["onenumcoin", "like", "%" . $onenumcoin . "%"];
               }
               //按单日排名发送名次数查找
               if ($rankdaynum = input("rankdaynum")) {
                   $where[] = ["rankdaynum", "like", "%" . $rankdaynum . "%"];
               }
               //按所有排名发送名称数查找
               if ($rankallnum = input("rankallnum")) {
                   $where[] = ["rankallnum", "like", "%" . $rankallnum . "%"];
               }
               //按创建时间查找
               $start = input("get.create_time-start");
               $end = input("get.create_time-end");
               if ($start && $end) {
                   $where[]=["create_time","between",[$start,date("Y-m-d",strtotime("$end +1 day"))]];
               }
        $list = M::onlyTrashed()->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
}
